<template>
	<view>
		<view class="bkk">
			<view class="bl">
				<view class="">头像</view>
				<view @tap="chooseImage()">
					<image style="width: 50px;height:50px;border-radius: 50px;" :src="imgList[0]" mode="aspectFill"
						v-if="imgList.length != 0" @tap="preview">
						<image style="width: 50px;height:50px;border-radius: 50px;" :src="pic" mode="aspectFill" v-else>
						</image>
				</view>
			</view>
		</view>
		<view class="bkkk" style="margin: 10px;">
			<view style="padding: 0 20px 0 20px;">
				<view class="bc h60 dx">
					<view class="r10" style="color: #333;">昵称</view><input type="text" v-model="username"/>
				</view>
				<view class="bc h60 dx">
					<view class="r10" style="color: #333;">年龄</view><input type="text" v-model="age"/>
				</view>
				<view class="bc h60">
					<view class="r10" style="color: #333;">性别</view>
					<select v-model="gender">
						<option value="男">男</option>
						<option value="女">女</option>
					</select>
				</view>
			</view>
		</view>
		<view class="" style="margin: 30px 10px;">
			<button @click="updateUserInfo()">修改</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgList: [],
				pic: '../../static/user.jpg',
				username: '张三',
				stationName: '',
				age: '',
				gender: '',
				headPath: ''
			}
		},
		methods: {
			// 预览图片
			ViewImage(e) {
				uni.previewImage({
					urls: _this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			//上传图片
			chooseImage() {
				let _this = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: function(res) {
						_this.imgList = res.tempFilePaths;
						uni.uploadFile({
							url: _this.baseURL + '/com/upload/pic',
							header: {
								'token': uni.getStorageSync('token')
							},
							filePath: _this.imgList[0],
							name: 'file',
							success: function(uploadFileRes) {
								console.log(uploadFileRes)
								if (uploadFileRes.statusCode == 200) {
									let fileData = JSON.parse(uploadFileRes.data)
									console.log(fileData.data.src);
									_this.headPath = fileData.data.src
								} else {
									uni.showToast({
										title: '上传图片不能超过2M，请重新上传',
										icon: 'error',
										duration: 2500
									})
									_this.imgList2 = [];
								}
							},
							fail: function(err) {
								console.log("请求失败\n" + JSON.stringify(err))
								uni.showToast({
									title: err.message,
									icon: 'error',
									duration: 2500
								})
							}
						});
					}
				});
			},


			// 获取用户信息
			getUserInfo() {
				let _this = this;
				uni.request({
					url: this.baseURL + '/find/user/info',
					method: 'POST',
					header: {
						token: uni.getStorageSync('token')
					},
					success: (res) => {
						let data = res.data;
						if (data.code === 0 || data.success == true) {
							let datas = data.data;
							_this.username = datas.username;
							_this.age = datas.age;
							_this.gender = datas.gender;
							if(datas.pic != null || datas.pic != undefined){
								//_this.pic = 'data:image/jpeg;base64,'+datas.pic;
								_this.pic = 'https://admin.yunshuicha.com'+datas.pic;
							}
						} else {
							uni.showToast({
								title: data.msg || data.message,
								icon: 'error',
								duration: 3000
							})
						}
					},
					fail: (e) => {
						console.warn(e)
					}
				});
			},
			updateUserInfo() {
				let _this = this;
				uni.request({
					url:_this.baseURL+'/update/u/info',
					method:'POST',
					header:{token:uni.getStorageSync('token')},
					data:{
						username: _this.username,
						pic: _this.headPath,
						age: _this.age,
						gender: _this.gender
					},
					success(ser){
						console.log(ser)
						if(ser.data.success == true){
							uni.showToast({
								title:'更新成功',
								icon:'success',
								duration:1500
							})
							setTimeout(function() {
								uni.redirectTo({
									url: "u-center"
								})
							}, 1500);
						}else{
							uni.showToast({
								title:'更新失败，'+ser.data.message,
								icon:'success',
								duration:2000
							})
						}
					},
					fail(err){
						console.log(err)
						uni.showToast({
							title:err.message,
							icon:'error',
							duration:3000
						})
					}
					
				})
			}
		},
		onLoad(p) {
			this.getUserInfo()
		}
	}
</script>
